<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="author" content="挥手の骑士">
    
    
    
    
    
    
    <title>跟上Java8 时间的魔术 | 骑士の物语</title>
    <link href="https://yihuishou.gitee.io" rel="prefetch">

    <link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/aos.css">
<link rel="stylesheet" href="/css/style.css">
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/aos.js"></script>
    <script src="/js/highslide/highslide-full.min.js"></script>
    <link rel="stylesheet" href="/js/highslide/highslide.css">
    <style type="text/css">
        @media (max-width: 768px) {
            body {
                background-color: #f0f0f0;
                background: url('/imgs/xsbg.gif');
                background-attachment: fixed;
            }
        }
    </style>
    
    <!--<script type="text/javascript">
      if (document.images) {
        var avatar = new Image();
        avatar.src = '/imgs/avatar.jpg'
        var previews = 'picHD_1.png,picHD_2.png,picHD_3.png,picHD_4.png,picHD_5.png,picHD_6.png,picHD_7.png,picHD_8.png,picHD_9.png,picHD_10.png,picHD_11.png,picHD_12.png,picHD_13.png,picHD_14.png,picHD_15.png,picHD_16.png,picHD_17.png,picHD_18.png,picHD_19.png,picHD_20.png,picHD_21.png,picHD_22.png,picHD_23.png,picHD_24.png,picHD_25.png,picHD_26.png,picHD_27.png,picHD_28.png,picHD_29.png,picHD_30.png,picHD_31.png,picHD_32.png,picHD_33.png,picHD_34.png,picHD_35.png,picHD_36.png,picHD_37.png,picHD_38.png,picHD_39.png,picHD_40.png,picHD_41.png,picHD_42.png'.split(',')
        var previewsPreLoad = []
        for(var i = 0; i < length; i++) {
          previewsPreLoad.push(new Image())
          previewsPreLoad[previewsPreLoad.length - 1].src = '/imgs/preview' + previews[i]
        }
      }
    </script>-->
</head></html>
<body>
    <!-- 背景轮播图功能 -->
    <section class="hidden-xs">
    <ul class="cb-slideshow">
        <li><span>天若</span></li>
        <li><span>有情</span></li>
        <li><span>天亦老</span></li>
        <li><span>我为</span></li>
        <li><span>长者</span></li>
        <li><span>续一秒</span></li>
    </ul>
</section>
    <!-- 欧尼酱功能, 谁用谁知道 -->
    
    <header class="navbar navbar-inverse" id="gal-header">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed"
                    data-toggle="collapse" data-target=".bs-navbar-collapse"
                    aria-expanded="false">
                <span class="fa fa-lg fa-reorder"></span>
            </button>
            <a href="https://yihuishou.gitee.io">
                
                <style>
                    #gal-header .navbar-brand {
                        height: 54px;
                        line-height: 24px;
                        font-size: 28px;
                        opacity: 1;
                        background-color: rgba(0,0,0,0);
                        text-shadow: 0 0 5px #fff,0 0 10px #fff,0 0 15px #fff,0 0 20px #228DFF,0 0 35px #228DFF,0 0 40px #228DFF,0 0 50px #228DFF,0 0 75px #228DFF;
                    }
                </style>
                <!-- 这里使用文字(navbar_text or config.title) -->
                <div class="navbar-brand">骑士の物语</div>
                
            </a>
        </div>
        <div class="collapse navbar-collapse bs-navbar-collapse">
            <ul class="nav navbar-nav" id="menu-gal">
                
                
                <li class="">
                    <a href="/">
                        <i class="fa fa-home"></i>首页
                    </a>
                </li>
                
                
                
                <li class="">
                    <a href="/archives">
                        <i class="fa fa-archive"></i>归档
                    </a>
                </li>
                
                
            </ul>
        </div>
    </div>
</header>
    <div id="gal-body">
        <div class="container">
            <div class="row">
                <div class="col-md-8 gal-right" id="mainstay">
                    
<article class="article well article-body" id="article">
    <div class="breadcrumb">
        <i class="fa fa-home"></i>
        <a href="https://yihuishou.gitee.io">骑士の物语</a>
        >
        <span>跟上Java8 时间的魔术</span>
    </div>
    <!-- 大型设备详细文章 -->
    <div class="hidden-xs">
        <div class="title-article">
            <h1>
                <a href="/2018/09/01/3165008964/">跟上Java8 时间的魔术</a>
            </h1>
        </div>
        <div class="tag-article">
            
            <span class="label label-gal">
                <i class="fa fa-tags"></i>
                
                <a href="/tags/Java/">Java</a>
                
            </span>
            
            <span class="label label-gal">
                <i class="fa fa-calendar"></i> 2018-09-01
            </span>
            
        </div>
    </div>
    <!-- 小型设备详细文章 -->
    <div class="visible-xs">
        <center>
            <div class="title-article">
                <h4>
                    <a href="/2018/09/01/3165008964/">跟上Java8 时间的魔术</a>
                </h4>
            </div>
            <p>
                <i class="fa fa-calendar"></i> 2018-09-01
            </p>
            <p>
                
                <i class="fa fa-tags"></i>
                
                <a href="/tags/Java/">Java</a>
                
                
                
            </p>
        </center>
    </div>
    <div class="content-article">
        <p>1、创建</p>
<p>根据年、月、日、时、分、秒、纳秒等创建LocalDateTime</p>
<p>eg：<br>LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00<br>LocalTime mid = LocalTime.parse(“12:00:00”); // 12:00:00<br>LocalTime now = LocalTime.now(); // 23:11:08.006</p>
<p>all method<br>LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute)<br>LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)<br>LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)<br>LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)<br>LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)<br>LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)<br>LocalDateTime of(LocalDate date, LocalTime time)</p>
<p>2、LocalDatetime 的所有方法：</p>
<p>eg：<br>// 取当前日期：<br>LocalDate today = LocalDate.now(); // -&gt; 2014-12-24<br>// 根据年月日取日期：<br>LocalDate crischristmas = LocalDate.of(2014, 12, 25); // -&gt; 2014-12-25<br>// 根据字符串取：<br>LocalDate endOfFeb = LocalDate.parse(“2014-02-28”); // 严格按照ISO yyyy-MM-dd验证，02写成2都不行，当然也有一个重载方法允许自己定义格式<br>LocalDate.parse(“2014-02-29”); // 无效日期无法通过：DateTimeParseException: Invalid date<br>// 取本月第1天：<br>LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2017-03-01<br>// 取本月第2天：<br>LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2017-03-02<br>// 取本月最后一天，再也不用计算是28，29，30还是31：<br>LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2017-12-31<br>// 取下一天：<br>LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); // 变成了2018-01-01<br>// 取2017年1月第一个周一，用Calendar要死掉很多脑细胞：<br>LocalDate firstMondayOf2015 = LocalDate.parse(“2017-01-01”).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02</p>
<p>all method:</p>
<ol>
<li>adjustInto    调整指定的Temporal和当前LocalDateTime对</li>
<li>atOffset    结合LocalDateTime和ZoneOffset创建一个</li>
<li>atZone    结合LocalDateTime和指定时区创建一个ZonedD</li>
<li>compareTo    比较两个LocalDateTime</li>
<li>format    格式化LocalDateTime生成一个字符串</li>
<li>from    转换TemporalAccessor为LocalDateTi</li>
<li>get    得到LocalDateTime的指定字段的值</li>
<li>getDayOfMonth    得到LocalDateTime是月的第几天</li>
<li>getDayOfWeek    得到LocalDateTime是星期几</li>
<li>getDayOfYear    得到LocalDateTime是年的第几天</li>
<li>getHour    得到LocalDateTime的小时</li>
<li>getLong    得到LocalDateTime指定字段的值</li>
<li>getMinute    得到LocalDateTime的分钟</li>
<li>getMonth    得到LocalDateTime的月份</li>
<li>getMonthValue    得到LocalDateTime的月份，从1到12</li>
<li>getNano    得到LocalDateTime的纳秒数</li>
<li>getSecond    得到LocalDateTime的秒数</li>
<li>getYear    得到LocalDateTime的年份</li>
<li>isAfter    判断LocalDateTime是否在指定LocalDateT</li>
<li>isBefore    判断LocalDateTime是否在指定LocalDateT</li>
<li>isEqual    判断两个LocalDateTime是否相等</li>
<li>isSupported    判断LocalDateTime是否支持指定时间字段或单元</li>
<li>minus    返回LocalDateTime减去指定数量的时间得到的值</li>
<li>minusDays    返回LocalDateTime减去指定天数得到的值</li>
<li>minusHours    返回LocalDateTime减去指定小时数得到的值</li>
<li>minusMinutes    返回LocalDateTime减去指定分钟数得到的值</li>
<li>minusMonths    返回LocalDateTime减去指定月数得到的值</li>
<li>minusNanos    返回LocalDateTime减去指定纳秒数得到的值</li>
<li>minusSeconds    返回LocalDateTime减去指定秒数得到的值</li>
<li>minusWeeks    返回LocalDateTime减去指定星期数得到的值</li>
<li>minusYears    返回LocalDateTime减去指定年数得到的值</li>
<li>now    返回指定时钟的当前LocalDateTime</li>
<li>of    根据年、月、日、时、分、秒、纳秒等创建LocalDateTi</li>
<li>ofEpochSecond    根据秒数(从1970-01-0100:00:00开始)创建L</li>
<li>ofInstant    根据Instant和ZoneId创建LocalDateTim</li>
<li>parse    解析字符串得到LocalDateTime</li>
<li>plus    返回LocalDateTime加上指定数量的时间得到的值</li>
<li>plusDays    返回LocalDateTime加上指定天数得到的值</li>
<li>plusHours    返回LocalDateTime加上指定小时数得到的值</li>
<li>plusMinutes    返回LocalDateTime加上指定分钟数得到的值</li>
<li>plusMonths    返回LocalDateTime加上指定月数得到的值</li>
<li>plusNanos    返回LocalDateTime加上指定纳秒数得到的值</li>
<li>plusSeconds    返回LocalDateTime加上指定秒数得到的值</li>
<li>plusWeeks    返回LocalDateTime加上指定星期数得到的值</li>
<li>plusYears    返回LocalDateTime加上指定年数得到的值</li>
<li>query    查询LocalDateTime</li>
<li>range    返回指定时间字段的范围</li>
<li>toLocalDate    返回LocalDateTime的LocalDate部分</li>
<li>toLocalTime    返回LocalDateTime的LocalTime部分</li>
<li>toString    返回LocalDateTime的字符串表示</li>
<li>truncatedTo    返回LocalDateTime截取到指定时间单位的拷贝</li>
<li>until    计算LocalDateTime和另一个LocalDateTi</li>
<li>with    返回LocalDateTime指定字段更改为新值后的拷贝</li>
<li>withDayOfMonth    返回LocalDateTime月的第几天更改为新值后的拷贝</li>
<li>withDayOfYear    返回LocalDateTime年的第几天更改为新值后的拷贝</li>
<li>withHour    返回LocalDateTime的小时数更改为新值后的拷贝</li>
<li>withMinute    返回LocalDateTime的分钟数更改为新值后的拷贝</li>
<li>withMonth    返回LocalDateTime的月份更改为新值后的拷贝</li>
<li>withNano    返回LocalDateTime的纳秒数更改为新值后的拷贝</li>
<li>withSecond    返回LocalDateTime的秒数更改为新值后的拷贝</li>
<li>withYear    返回LocalDateTime年份更改为新值后的拷贝</li>
</ol>
<p>3、对应的SQL的类型</p>
<p>SQL -&gt; Java</p>
<p>date -&gt; LocalDate<br>time -&gt; LocalTime<br>timestamp -&gt; LocalDateTime</p>
<p>4、根据上面的方法自定义的Util类</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DateTimeUtils</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(<span class="string">"HHmmss"</span>);</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern(<span class="string">"yyyyMM"</span>);</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern(<span class="string">"yyMMdd"</span>);</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern(<span class="string">"yyMMddHHmmss"</span>);</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> DateTimeFormatter DATETIME_FORMATTER =  DateTimeFormatter.ofPattern(<span class="string">"yyyyMMddHHmmss"</span>);</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(<span class="string">"yyyyMMdd"</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回当前的日期</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalDate <span class="title">getCurrentLocalDate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.now();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回当前时间</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalTime <span class="title">getCurrentLocalTime</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalTime.now();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回当前日期时间</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title">getCurrentLocalDateTime</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDateTime.now();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * yyyyMMdd</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentDateStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.now().format(DATE_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * yyMMdd</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentShortDateStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.now().format(SHORT_DATE_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentMonthStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.now().format(MONTH_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * yyyyMMddHHmmss</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentDateTimeStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDateTime.now().format(DATETIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * yyMMddHHmmss</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentShortDateTimeStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDateTime.now().format(SHORT_DATETIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * HHmmss</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentTimeStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalTime.now().format(TIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentDateStr</span><span class="params">(String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.now().format(DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentDateTimeStr</span><span class="params">(String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentTimeStr</span><span class="params">(String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalDate <span class="title">parseLocalDate</span><span class="params">(String dateStr, String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title">parseLocalDateTime</span><span class="params">(String dateTimeStr, String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalTime <span class="title">parseLocalTime</span><span class="params">(String timeStr, String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatLocalDate</span><span class="params">(LocalDate date, String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> date.format(DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatLocalDateTime</span><span class="params">(LocalDateTime datetime, String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> datetime.format(DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatLocalTime</span><span class="params">(LocalTime time, String pattern)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> time.format(DateTimeFormatter.ofPattern(pattern));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalDate <span class="title">parseLocalDate</span><span class="params">(String dateStr)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDate.parse(dateStr, DATE_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title">parseLocalDateTime</span><span class="params">(String dateTimeStr)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalDateTime.parse(dateTimeStr, DATETIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> LocalTime <span class="title">parseLocalTime</span><span class="params">(String timeStr)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> LocalTime.parse(timeStr, TIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatLocalDate</span><span class="params">(LocalDate date)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> date.format(DATE_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatLocalDateTime</span><span class="params">(LocalDateTime datetime)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> datetime.format(DATETIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatLocalTime</span><span class="params">(LocalTime time)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> time.format(TIME_FORMATTER);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 日期相隔天数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> startDateInclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> endDateExclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">periodDays</span><span class="params">(LocalDate startDateInclusive, LocalDate endDateExclusive)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Period.between(startDateInclusive, endDateExclusive).getDays();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 日期相隔小时</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> startInclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> endExclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">durationHours</span><span class="params">(Temporal startInclusive, Temporal endExclusive)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Duration.between(startInclusive, endExclusive).toHours();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 日期相隔分钟</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> startInclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> endExclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">durationMinutes</span><span class="params">(Temporal startInclusive, Temporal endExclusive)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Duration.between(startInclusive, endExclusive).toMinutes();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 日期相隔毫秒数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> startInclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> endExclusive</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">durationMillis</span><span class="params">(Temporal startInclusive, Temporal endExclusive)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Duration.between(startInclusive, endExclusive).toMillis();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 是否当天</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> date</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isToday</span><span class="params">(LocalDate date)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getCurrentLocalDate().equals(date);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Long <span class="title">toEpochMilli</span><span class="params">(LocalDateTime dateTime)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Date与LocalDate的互转</p>
<p>Date转LocalDate：</p>
<p>Date date = new Date();<br>LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();</p>
<p>LocalDate 转 Date:</p>
<p>LocalDateTime localDateTime = LocalDateTime.now();<br>Date date = Date.from(localDateTime.toInstant(ZoneOffset.UTC))</p>
<p>Timestamp与LocalDate的互转</p>
<p>Long timestamp = LocalDateTime.now().toInstant(ZoneOffset.of(“+8”)).toEpochMilli();<br>LocalDateTime time2 =LocalDateTime.ofEpochSecond(timestamp/1000,0,ZoneOffset.ofHours(8));</p>

    </div>
</article>


                </div>
                <aside class="col-md-4 gal-left" id="sidebar">
    <!-- 此为sidebar的搜索框, 非搜索结果页面 -->
<aside id="sidebar-search">
    <div class="search hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <form class="form-inline clearfix" id="search-form" method="get"
              action="/search/index.html">
            <input type="text" name="s" class="form-control" id="searchInput" placeholder="搜索文章~" autocomplete="off">
            <button class="btn btn-danger btn-gal" type="submit">
                <i class="fa fa-search"></i>
            </button>
        </form>
    </div>
</aside>
    <aside id="sidebar-author">
    <div class="panel panel-gal" data-aos="flip-right" data-aos-duration="3000">
        <div class="panel-heading" style="text-align: center">
            <i class="fa fa-quote-left"></i>
            挥手の骑士
            <i class="fa fa-quote-right"></i>
        </div>
        <div class="author-panel text-center">
            <img src="/imgs/avatar.jpg" width="140" height="140"
                 alt="个人头像" class="author-image">
            <p class="author-description"></p>
        </div>
    </div>
</aside>
    
    <!-- 要配置好leancloud才能开启此小工具 -->
    
    
    <aside id="sidebar-recent_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            近期文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/08/18/55226946/">agent v3.2.1 插件激活码</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/22/2412014068/">干掉 CMS 未来属于 ZGC</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/21/4075219928/">OpenJDK 和 OracleJDK 的抉择</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/20/368594680/">Flutter 音乐播放器开发之路（一）</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/15/3650854255/">XaaS 到底是什么？</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/15/4036766117/">让 Windows 10 开机自动登录</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/14/466733536/">给 Flutter 项目初始化构建提个速</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/06/12/4125727764/">JavaScript 中 Splice 函数与数组塌陷</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/06/12/1586694155/">在 Java 中缩写 Lambda 表达式</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/06/04/2276828982/">博客文章规范</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="sidebar-rand_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            随机文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/05/28/1395599601/">Four pillars for US hegemony</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2018/05/02/2824090707/">IDEA 性能调校</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2018/06/05/167649745/">JVM 调优基本垃圾回收</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/09/30/4240243490/">MDUI v1版本的迁移记录</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2018/12/21/3764698259/">Mybatis 中注解式多表关联查询</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2018/08/20/3328494696/">Vue-Router 看这一篇就够了</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2018/09/04/3950067579/">优雅地使用枚举</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2017/09/27/3303306385/">关于 Mdui 主题中的一些设置问题</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/30/2948278912/">复利原理</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2017/10/18/2023601047/">汉化 IDEA 的各种坑</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="gal-sets">
        <div class="panel panel-gal hidden-xs" data-aos="fade-up" data-aos-duration="2000">
            <ul class="nav nav-pills pills-gal">

                
                <li>
                    <a href="/2018/09/01/3165008964/index.html#sidebar-tags" data-toggle="tab" id="tags-tab">热门标签</a>
                </li>
                
                
                <li>
                    <a href="/2018/09/01/3165008964/index.html#sidebar-friend-links" data-toggle="tab" id="friend-links-tab">友情链接</a>
                </li>
                
                
                <li>
                    <a href="/2018/09/01/3165008964/index.html#sidebar-links" data-toggle="tab" id="links-tab">个人链接</a>
                </li>
                
            </ul>
            <div class="tab-content">
                
                <div class="cloud-tags tab-pane nav bs-sidenav fade" id="sidebar-tags">
    
    <a href="/tags/SpringSecurity/" style="font-size: 14.01909969933594px;" class="tag-cloud-link">SpringSecurity</a>
    
    <a href="/tags/Xml/" style="font-size: 13.883596865570237px;" class="tag-cloud-link">Xml</a>
    
    <a href="/tags/随笔/" style="font-size: 8.080928305167689px;" class="tag-cloud-link">随笔</a>
    
    <a href="/tags/大前端/" style="font-size: 10.895877729988623px;" class="tag-cloud-link">大前端</a>
    
    <a href="/tags/Dart/" style="font-size: 8.75851760305887px;" class="tag-cloud-link">Dart</a>
    
    <a href="/tags/Flutter/" style="font-size: 13.397349497132357px;" class="tag-cloud-link">Flutter</a>
    
    <a href="/tags/Go/" style="font-size: 10.571855547891925px;" class="tag-cloud-link">Go</a>
    
    <a href="/tags/IDEA/" style="font-size: 18.38273038264375px;" class="tag-cloud-link">IDEA</a>
    
    <a href="/tags/Java/" style="font-size: 14.48116595605001px;" class="tag-cloud-link">Java</a>
    
    <a href="/tags/JavaScript/" style="font-size: 13.649926929013366px;" class="tag-cloud-link">JavaScript</a>
    
    <a href="/tags/Mysql/" style="font-size: 15.311875454419622px;" class="tag-cloud-link">Mysql</a>
    
    <a href="/tags/Oracle/" style="font-size: 15.871997739785876px;" class="tag-cloud-link">Oracle</a>
    
    <a href="/tags/Photoshop/" style="font-size: 15.246790386027543px;" class="tag-cloud-link">Photoshop</a>
    
    <a href="/tags/RPG-Maker-MV/" style="font-size: 17.416516245781793px;" class="tag-cloud-link">RPG Maker MV</a>
    
    <a href="/tags/Restful/" style="font-size: 17.08269583276847px;" class="tag-cloud-link">Restful</a>
    
    <a href="/tags/Redis/" style="font-size: 15.867078805116124px;" class="tag-cloud-link">Redis</a>
    
    <a href="/tags/SQL/" style="font-size: 18.475393377798085px;" class="tag-cloud-link">SQL</a>
    
    <a href="/tags/SpringBoot/" style="font-size: 17.45916942544919px;" class="tag-cloud-link">SpringBoot</a>
    
    <a href="/tags/Swagger/" style="font-size: 14.822614948701622px;" class="tag-cloud-link">Swagger</a>
    
    <a href="/tags/Windows/" style="font-size: 15.247500077924705px;" class="tag-cloud-link">Windows</a>
    
    <a href="/tags/Deepleanring/" style="font-size: 18.17087196513016px;" class="tag-cloud-link">Deepleanring</a>
    
    <a href="/tags/Vue/" style="font-size: 17.583968479592176px;" class="tag-cloud-link">Vue</a>
    
    <a href="/tags/Docker/" style="font-size: 10.671448955940539px;" class="tag-cloud-link">Docker</a>
    
    <a href="/tags/Hexo/" style="font-size: 10.303349168993677px;" class="tag-cloud-link">Hexo</a>
    
    <a href="/tags/VsCode/" style="font-size: 10.270366514257027px;" class="tag-cloud-link">VsCode</a>
    
    <a href="/tags/SpringCloud/" style="font-size: 16.23126127664978px;" class="tag-cloud-link">SpringCloud</a>
    
</div>
                
                
                <div class="friend-links tab-pane nav bs-sidenav fade" id="sidebar-friend-links">
    
    <li>
        <a href="http://noodlefighter.com" target="_blank">虾包</a>
    </li>
    
    <li>
        <a href="https://loli.la" target="_blank">超级叫了</a>
    </li>
    
    <li>
        <a href="https://konata9.github.io" target="_blank">吃土的小此方</a>
    </li>
    
</div>
                
                
                <div class="links tab-pane nav bs-sidenav fade" id="sidebar-links">
    
    <li>
        <a href="https://github.com/yihuishou" target="_blank">Github</a>
    </li>
    
    <li>
        <a href="https://gite.com/yihuishou" target="_blank">码云</a>
    </li>
    
    <li>
        <a href="https://www.zhihu.com/people/hei-yi-tian-shi-48" target="_blank">知乎</a>
    </li>
    
</div>
                
            </div>
        </div>
    </aside>
    
</aside>
            </div>
        </div>
    </div>
    <footer id="gal-footer">
    <div class="container">
        Copyright © 2018 挥手の骑士 Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>.&nbsp;Theme by <a href="https://github.com/ZEROKISEKI" target="_blank">AONOSORA</a>
    </div>
</footer>

<!-- 回到顶端 -->
<div id="gal-gotop">
    <i class="fa fa-angle-up"></i>
</div>
</body>
<script src="/js/activate-power-mode.js"></script>
<script>

    // 配置highslide
	hs.graphicsDir = '/js/highslide/graphics/'
    hs.outlineType = "rounded-white";
    hs.dimmingOpacity = 0.8;
    hs.outlineWhileAnimating = true;
    hs.showCredits = false;
    hs.captionEval = "this.thumb.alt";
    hs.numberPosition = "caption";
    hs.align = "center";
    hs.transitions = ["expand", "crossfade"];
    hs.lang.number = '共%2张图, 当前是第%1张';
    hs.addSlideshow({
      interval: 5000,
      repeat: true,
      useControls: true,
      fixedControls: "fit",
      overlayOptions: {
        opacity: 0.75,
        position: "bottom center",
        hideOnMouseOut: true
      }
    })

    // 初始化aos
    AOS.init({
      duration: 1000,
      delay: 0,
      easing: 'ease-out-back'
    });

</script>
<script>
	POWERMODE.colorful = 'true';    // make power mode colorful
	POWERMODE.shake = 'true';       // turn off shake
	// TODO 这里根据具体情况修改
	document.body.addEventListener('input', POWERMODE);
</script>
<script>
    window.slideConfig = {
      prefix: '/imgs/slide/background',
      ext: 'jpg',
      maxCount: '6'
    }
</script>
<script src="/js/hs.js"></script>
<script src="/js/blog.js"></script>



</html>